package com.shlh.saas.mapper.facebook;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.shlh.saas.entity.facebook.FacebookUser;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * Facebook用户数据访问接口
 */
@Mapper
public interface FacebookUserMapper extends BaseMapper<FacebookUser> {

    /**
     * 在所有字段中搜索关键词（带分页）
     * ShardingSphere会根据分片键自动路由到正确的分片表
     */
    @Select("SELECT * FROM facebook_users WHERE (" +
            "first_name LIKE CONCAT('%', #{keyword}, '%') OR " +
            "last_name LIKE CONCAT('%', #{keyword}, '%') OR " +
            "email LIKE CONCAT('%', #{keyword}, '%') OR " +
            "gender LIKE CONCAT('%', #{keyword}, '%') OR " +
            "work_place LIKE CONCAT('%', #{keyword}, '%') OR " +
            "workplace_city LIKE CONCAT('%', #{keyword}, '%') OR " +
            "id LIKE CONCAT('%', #{keyword}, '%')) " +
            "LIMIT #{limit}")
    List<FacebookUser> searchInAllFields(
        @Param("keyword") String keyword,
        @Param("limit") int limit
    );

    /**
     * 根据ID精确查询（用于分片路由测试）
     * 当提供具体的id时，ShardingSphere能够精确路由到对应分片
     */
    @Select("SELECT * FROM facebook_users WHERE id = #{id}")
    FacebookUser findById(@Param("id") String id);

    /**
     * 统计在所有字段中包含关键词的记录总数
     * ShardingSphere会自动在所有分片中执行并汇总结果
     */
    @Select("SELECT COUNT(*) FROM facebook_users WHERE (" +
            "first_name LIKE CONCAT('%', #{keyword}, '%') OR " +
            "last_name LIKE CONCAT('%', #{keyword}, '%') OR " +
            "email LIKE CONCAT('%', #{keyword}, '%') OR " +
            "gender LIKE CONCAT('%', #{keyword}, '%') OR " +
            "work_place LIKE CONCAT('%', #{keyword}, '%') OR " +
            "workplace_city LIKE CONCAT('%', #{keyword}, '%') OR " +
            "id LIKE CONCAT('%', #{keyword}, '%')) ")
    long countInAllFields(@Param("keyword") String keyword);

    /**
     * 在所有字段中搜索关键词（带分页）
     * ShardingSphere会自动在所有分片中执行查询
     */
    @Select("SELECT * FROM facebook_users WHERE " +
            "first_name LIKE CONCAT('%', #{keyword}, '%') OR " +
            "last_name LIKE CONCAT('%', #{keyword}, '%') OR " +
            "email LIKE CONCAT('%', #{keyword}, '%') OR " +
            "gender LIKE CONCAT('%', #{keyword}, '%') OR " +
            "work_place LIKE CONCAT('%', #{keyword}, '%') OR " +
            "workplace_city LIKE CONCAT('%', #{keyword}, '%') OR " +
            "id LIKE CONCAT('%', #{keyword}, '%')) " +
            "LIMIT #{offset}, #{limit}")
    List<FacebookUser> searchInAllFieldsWithPagination(
        @Param("keyword") String keyword,
        @Param("offset") int offset,
        @Param("limit") int limit
    );
    
    /**
     * 查询所有数据（带分页）
     * ShardingSphere会自动在所有分片中执行查询并合并结果
     */
    @Select("SELECT * FROM facebook_users LIMIT #{offset}, #{limit}")
    List<FacebookUser> selectAllWithPagination(
        @Param("offset") int offset,
        @Param("limit") int limit
    );
    
    /**
     * 统计总记录数
     * ShardingSphere会自动在所有分片中执行并汇总结果
     */
    @Select("SELECT COUNT(*) FROM facebook_users")
    long countAll();
    
    /**
     * 查找相似的姓名
     * ShardingSphere会在所有分片中查找并去重
     */
    @Select("SELECT DISTINCT CONCAT(first_name, ' ', last_name) AS name FROM facebook_users WHERE first_name LIKE CONCAT('%', #{keyword}, '%') OR last_name LIKE CONCAT('%', #{keyword}, '%') LIMIT 5")
    List<String> findSimilarNames(@Param("keyword") String keyword);

    /**
     * 查找相似的位置
     * ShardingSphere会在所有分片中查找并去重
     */
    @Select("SELECT DISTINCT work_place FROM facebook_users WHERE work_place LIKE CONCAT('%', #{keyword}, '%') LIMIT 5")
    List<String> findSimilarLocations(@Param("keyword") String keyword);

    /**
     * 通过关键词搜索具有电子邮箱的Facebook用户
     * 
     * @param keyword 搜索关键词
     * @param offset 偏移量
     * @param limit 限制数量
     * @return 符合条件的Facebook用户列表
     */
    @Select("<script>" +
            "SELECT * FROM facebook_users WHERE email IS NOT NULL AND email != '' " +
            "AND (first_name LIKE CONCAT('%',#{keyword},'%') " +
            "OR last_name LIKE CONCAT('%',#{keyword},'%') " +
            "OR id LIKE CONCAT('%',#{keyword},'%') " +
            "OR work_place LIKE CONCAT('%',#{keyword},'%')) " +
            "ORDER BY id " +
            "LIMIT #{limit} OFFSET #{offset}" +
            "</script>")
    List<FacebookUser> searchFacebookUsersWithEmail(@Param("keyword") String keyword, 
                                                   @Param("offset") int offset, 
                                                   @Param("limit") int limit);
    
    /**
     * 统计通过关键词搜索具有电子邮箱的Facebook用户数量
     * 
     * @param keyword 搜索关键词
     * @return 符合条件的用户数量
     */
    @Select("<script>" +
            "SELECT COUNT(*) FROM facebook_users WHERE email IS NOT NULL AND email != '' " +
            "AND (first_name LIKE CONCAT('%',#{keyword},'%') " +
            "OR last_name LIKE CONCAT('%',#{keyword},'%') " +
            "OR id LIKE CONCAT('%',#{keyword},'%') " +
            "OR work_place LIKE CONCAT('%',#{keyword},'%'))" +
            "</script>")
    int countFacebookUsersWithEmail(@Param("keyword") String keyword);
    
    /**
     * 通过关键词搜索具有电话的Facebook用户
     * 
     * @param keyword 搜索关键词
     * @param offset 偏移量
     * @param limit 限制数量
     * @return 符合条件的Facebook用户列表
     */
    @Select("<script>" +
            "SELECT * FROM facebook_users WHERE id IS NOT NULL AND id != '' " +
            "AND (first_name LIKE CONCAT('%',#{keyword},'%') " +
            "OR last_name LIKE CONCAT('%',#{keyword},'%') " +
            "OR email LIKE CONCAT('%',#{keyword},'%') " +
            "OR work_place LIKE CONCAT('%',#{keyword},'%')) " +
            "ORDER BY id " +
            "LIMIT #{limit} OFFSET #{offset}" +
            "</script>")
    List<FacebookUser> searchFacebookUsersWithPhone(@Param("keyword") String keyword, 
                                                   @Param("offset") int offset, 
                                                   @Param("limit") int limit);
    
    /**
     * 统计通过关键词搜索具有电话的Facebook用户数量
     * 
     * @param keyword 搜索关键词
     * @return 符合条件的用户数量
     */
    @Select("<script>" +
            "SELECT COUNT(*) FROM facebook_users WHERE id IS NOT NULL AND id != '' " +
            "AND (first_name LIKE CONCAT('%',#{keyword},'%') " +
            "OR last_name LIKE CONCAT('%',#{keyword},'%') " +
            "OR email LIKE CONCAT('%',#{keyword},'%') " +
            "OR work_place LIKE CONCAT('%',#{keyword},'%'))" +
            "</script>")
    int countFacebookUsersWithPhone(@Param("keyword") String keyword);

    /**
     * 根据邮箱精确查询Facebook用户
     * 
     * @param email 邮箱地址
     * @return Facebook用户列表
     */
    @Select("SELECT * FROM facebook_users WHERE email = #{email} LIMIT 10")
    List<FacebookUser> findByEmail(@Param("email") String email);
    
    /**
     * 根据电话号码精确查询Facebook用户
     * 
     * @param phoneNumber 电话号码
     * @return Facebook用户
     */
    @Select("SELECT * FROM facebook_users WHERE id = #{phoneNumber} LIMIT 1")
    FacebookUser findByPhoneNumber(@Param("phoneNumber") String phoneNumber);

    @Select("<script>" +
            "SELECT SUM(cnt) FROM (" +
            "  <foreach collection='shardIndices' item='shard' separator=' UNION ALL '>" +
            "    SELECT COUNT(*) AS cnt FROM data_#{shard} " +
            "    <where>" +" ${ew.customSqlSegment}" + " </where>"+
            "  </foreach>" +
            ") AS totals" +
            "</script>")
    Long countByShardIndices(
            @Param("ew") LambdaQueryWrapper<FacebookUser> wrapper,
            @Param("shardIndices") List<Integer> shardIndices
    );
} 